﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

namespace Enter.Common.validcode
{
    public class ValidPic
    {
        private Color _backColor;
        private Color _fontColor;
        private string _fontName;
        private int _fontsize;
        private int _height;
        private string _randnum;
        private int _width;
        private Font font;
        private Graphics g = null;
        private Bitmap newBitmap = null;

        public void DrawValidPic()
        {
            this.newBitmap = new Bitmap(this._width, this._height, PixelFormat.Format32bppArgb);
            this.g = Graphics.FromImage(this.newBitmap);
            this.font = new Font(this._fontName, (float)this._fontsize, FontStyle.Bold);
            this.g.FillRectangle(new SolidBrush(this._backColor), new Rectangle(0, 0, this._width, this._height));
            this.g.DrawString(this._randnum, this.font, new SolidBrush(this._fontColor), (float)0f, (float)0f);
            MemoryStream stream = new MemoryStream();
            this.newBitmap.Save(stream, ImageFormat.Png);
            this.newBitmap.Dispose();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/GIF";
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            HttpContext.Current.Response.End();
        }

        public Color BackColor
        {
            set
            {
                this._backColor = value;
            }
        }

        public Color FontColor
        {
            set
            {
                this._fontColor = value;
            }
        }

        public string FontName
        {
            set
            {
                this._fontName = value;
            }
        }

        public int Fontsize
        {
            set
            {
                this._fontsize = value;
            }
        }

        public int Height
        {
            set
            {
                this._height = value;
            }
        }

        public string Randnum
        {
            set
            {
                this._randnum = value;
            }
        }

        public int Width
        {
            set
            {
                this._width = value;
            }
        }
    }
}
